//
//  QGUITabBarController.m
//  QuanGou
//
//  Created by lanou3g on 15/11/17.
//  Copyright © 2015年 LiuFei. All rights reserved.
//

#import "QGUITabBarController.h"
#import "QGShowPageViewController.h"
#import "QGShopingPageViewController.h"
#import "QGTalkPageViewController.h"
#import "QGLoginPageViewController.h"
#import "QGUserPageViewController.h"
#import "QGHelpPageViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "QGTabBar.h"

@interface QGUITabBarController ()<CLLocationManagerDelegate, QGTabBarDelegate>
@property (nonatomic ,strong) CLLocationManager *locationManager;

@end

@implementation QGUITabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor magentaColor]];
    // Do any additional setup after loading the view.
    QGHelpPageViewController *helpPageVC = [[QGHelpPageViewController alloc] initWithNibName:@"QGHelpPageViewController" bundle:[NSBundle mainBundle]];
//    QGShowPageViewController *showPageVC = [[QGShowPageViewController alloc]initWithNibName:@"QGShowPageViewController" bundle:[NSBundle mainBundle]];
    QGTalkPageViewController *talkPageVC = [[QGTalkPageViewController alloc]initWithNibName:@"QGTalkPageViewController" bundle:[NSBundle mainBundle]];
    QGUserPageViewController *userPageVC = [[QGUserPageViewController alloc]initWithNibName:@"QGUserPageViewController" bundle:[NSBundle mainBundle]];
    QGShopingPageViewController *shopingPageVC = [[QGShopingPageViewController alloc]initWithNibName:@"QGShopingPageViewController" bundle:[NSBundle mainBundle]];
    shopingPageVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"home_normal"] tag:100];
    helpPageVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"帮帮帮" image:[UIImage imageNamed:@"home_loc"] tag:101];
    talkPageVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"会话" image:[UIImage imageNamed:@"icon_more"] tag:103];
//    UIImage *img = [[UIImage imageNamed:@"icon_post"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    userPageVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"share_qq_icon"] tag:104];
//    showPageVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"会话" image:img tag:102];
    [self setViewControllers:[NSArray arrayWithObjects:shopingPageVC,helpPageVC,talkPageVC,userPageVC, nil]];
    [self setSelectedViewController:shopingPageVC];
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"header_bg"]];
    
    [self setupSupViews];
    
    

    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager requestAlwaysAuthorization];
    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];
    
}
- (void)setupSupViews {
    QGTabBar *customTabBar = [[QGTabBar alloc] init];
    customTabBar.tabBarDelegate = self;
    // 更换系统自带的tabbar
    [self setValue:customTabBar forKeyPath:@"tabBar"];
}
#pragma mark - PLTabBarDelegate
- (void)tabBarDidClick:(QGTabBar *)tabBar
{
    QGShowPageViewController *showPageVC = [[QGShowPageViewController alloc] initWithNibName:@"QGShowPageViewController" bundle:[NSBundle mainBundle]];
    [self presentViewController:showPageVC animated:YES completion:nil];
}

# pragma mark - 获取地理位置 Delegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:[locations firstObject] completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *placeMark = [placemarks lastObject];
        NSDictionary *addressDic = [placeMark addressDictionary];
        
        if ([addressDic[@"FormattedAddressLines"] lastObject] != nil) {
            [self.locationManager stopUpdatingLocation];
            
            AVGeoPoint *userLocation = [AVGeoPoint geoPointWithLatitude:[locations firstObject].coordinate.latitude longitude:[locations firstObject].coordinate.longitude];
            AVQuery *query = [AVQuery queryWithClassName:@"UserModel"];
            [query whereKey:@"userId" equalTo:[AVUser currentUser].objectId];
            [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
                if (!object) {
                    NSLog(@"getFirstObject 请求失败。");
                } else {
                    // 查询成功
                    NSLog(@"对象成功返回。");
                    // 知道 objectId，创建 AVObject
                    AVObject *post = [AVObject objectWithoutDataWithClassName:@"UserModel" objectId:object.objectId];
                    //更新属性
                    [post setObject:userLocation forKey:@"location"];
                    [post setObject:placeMark.locality forKey:@"locationCity"];
                    //保存
                    [post saveInBackground];
                }
            }];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
